package com.chuanyang.driverh.mainorder;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.chuanyang.driverh.BaseApplication;
import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.bean.AssignGoodData;
import com.chuanyang.driverh.mainorder.adapter.CarTeamMenmberAdapter;
import com.chuanyang.driverh.responseresult.CarTeamMenmberData;
import com.chuanyang.driverh.responseresult.CarTeamMenmberResult;
import com.chuanyang.driverh.responseresult.DriverInfoData;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class CarTeamMenmberActivity extends BaseTitleActivity implements AdapterView.OnItemClickListener {
    private CarTeamMenmberAdapter adapter;
    private ArrayList<CarTeamMenmberData> list;
    private DriverInfoData driverinfo;
    private String index;
    private Intent intent;
    private ArrayList<AssignGoodData> waybillParams;
    private ArrayList<CarTeamMenmberData> removeCarMenmbers;


    @Override
    protected int getContentResId() {
            return R.layout.order_act_carteammenmber;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

    }

    @Override
    protected void initData() {

        setBackImg();
        getTitleTextView().setText("车队成员");
        driverinfo = BaseApplication.getInstance().getDriverInfo();
        intent = getIntent();
        index = intent.getStringExtra("index");
        ListView lv_list = (ListView) findViewById(R.id.lv_catteammenmber_list);
        list = new ArrayList();
        removeCarMenmbers = new ArrayList<>();
        adapter = new CarTeamMenmberAdapter(this, list);
        lv_list.setAdapter(adapter);
        lv_list.setOnItemClickListener(this);
        selectFleetDriver();
    }

    @Override
    protected void initListener() {

    }



    public void selectFleetDriver() {
        DialogUtil.showLoadingDialog(activityContext,"加载中...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("fleetId", driverinfo.getFleet().getId() + "");
        params.put("fleetDriverState", "1");
        MyHttpUtil.selectFleetDriver(activityContext,params, new MyCallback<CarTeamMenmberResult>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(CarTeamMenmberResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    if (result.getData().size() > 0) {
                        list = result.getData();
                        //如果已经分配过，则列表里不显示
                        removeArrange(list);
                    }
                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });

    }

    private void removeArrange(ArrayList<CarTeamMenmberData> list) {
        adapter.setList(list);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        CarTeamMenmberData data = list.get(position);
        Intent intent1 = new Intent();
        intent1.putExtra("CarTeamMenmberData", data);
        if (index.equals("1")) {
            setResult(1, intent1);
        } else {
            setResult(2, intent1);
        }

        finish();
    }
}
